display: Avoid unsetting the DESKTOP_STARTUP_ID variable too late
authorMario Sanchez Prada <msanchez@gnome.org>
Mon, 11 Jun 2018 15:09:06 +0000 (16:09 +0100)
committerMario Sanchez Prada <msanchez@gnome.org>
Mon, 11 Jun 2018 15:28:24 +0000 (16:28 +0100)
Similar to what has been done recently for DESKTOP_AUTOSTART_ID [1],
we need to get rid of this call to g_unsetenv() in the displays'
backends for X11 and Wayland, so that it's guarantee to happen any
thread is created, while still being accessible when needed.

Let's stash the value of this environment variable when loading the
GDK library, and provide a private method so that it can be retrieved
from the displays' backend when implementing gdk_display_make_default().

[1] https://gitlab.gnome.org/GNOME/gtk/commit/22269902

Closes: https://gitlab.gnome.org/GNOME/gtk/issues/979
gdk/gdk-private.h
gdk/gdk.c
gdk/wayland/gdkdisplay-wayland.c
gdk/x11/gdkdisplay-x11.c

index b4a16c5659465ef82ab06a9572bd72e87f282dd9..c6a45ed48f4c299eedb2689e3bf1bab486da1835 100644 (file)
@@ -40,4 +40,6 @@ void gdk_display_set_cursor_theme          (GdkDisplay   *display,
                                             const char   *theme,
                                             int           size);
 
+const gchar *   gdk_get_startup_notification_id (void);
+
 #endif /* __GDK__PRIVATE_H__ */
index 72d1478f694dc932cfb69eb66b81014464d1086c..94d27ee87ed33a220186cbf0b73ee6841771b111 100644 (file)
--- a/gdk/gdk.c
+++ b/gdk/gdk.c
@@ -154,6 +154,36 @@ static const GDebugKey gdk_debug_keys[] = {
 };
 #endif
 
+
+#ifdef G_HAS_CONSTRUCTORS
+#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(stash_desktop_startup_notification_id)
+#endif
+G_DEFINE_CONSTRUCTOR(stash_desktop_startup_notification_id)
+#endif
+
+static gchar *startup_notification_id = NULL;
+
+static void
+stash_desktop_startup_notification_id (void)
+{
+  const char *desktop_startup_id;
+
+  desktop_startup_id = g_getenv ("DESKTOP_STARTUP_ID");
+  if (desktop_startup_id && *desktop_startup_id != '\0')
+    {
+      if (!g_utf8_validate (desktop_startup_id, -1, NULL))
+        g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
+      else
+        startup_notification_id = g_strdup (desktop_startup_id ? desktop_startup_id : "");
+    }
+
+  /* Clear the environment variable so it won't be inherited by
+   * child processes and confuse things.
+   */
+  g_unsetenv ("DESKTOP_STARTUP_ID");
+}
+
 static gpointer
 register_resources (gpointer dummy G_GNUC_UNUSED)
 {
@@ -186,6 +216,10 @@ gdk_pre_parse (void)
                                               G_N_ELEMENTS (gdk_debug_keys));
   }
 #endif  /* G_ENABLE_DEBUG */
+
+#ifndef G_HAS_CONSTRUCTORS
+  stash_desktop_startup_notification_id ();
+#endif
 }
 
 /*< private >
@@ -216,6 +250,22 @@ gdk_display_open_default (void)
   return display;
 }
 
+/*< private >
+ *
+ * gdk_get_startup_notification_id
+ *
+ * Returns the original value of the DESKTOP_STARTUP_ID environment
+ * variable if it was defined and valid, or %NULL otherwise.
+ *
+ * Returns: (nullable) (transfer none): the original value of the
+ *   DESKTOP_STARTUP_ID environment variable, or %NULL.
+ */
+const gchar *
+gdk_get_startup_notification_id (void)
+{
+  return startup_notification_id;
+}
+
 /**
  * SECTION:threads
  * @Short_description: Functions for using GDK in multi-threaded programs
index 3fb9fda050482091d4a23aa8de4a8fdba9d7eca3..2f46817be4835631245e16a21cd4a52abbe413ab 100644 (file)
@@ -54,6 +54,8 @@
 
 #include "wm-button-layout-translation.h"
 
+#include "gdk/gdk-private.h"
+
 /**
  * SECTION:wayland_interaction
  * @Short_description: Wayland backend-specific functions
@@ -817,19 +819,9 @@ gdk_wayland_display_make_default (GdkDisplay *display)
   g_free (display_wayland->startup_notification_id);
   display_wayland->startup_notification_id = NULL;
 
-  startup_id = g_getenv ("DESKTOP_STARTUP_ID");
-  if (startup_id && *startup_id != '\0')
-    {
-      if (!g_utf8_validate (startup_id, -1, NULL))
-        g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
-      else
-        display_wayland->startup_notification_id = g_strdup (startup_id);
-
-      /* Clear the environment variable so it won't be inherited by
-       * child processes and confuse things.
-       */
-      g_unsetenv ("DESKTOP_STARTUP_ID");
-    }
+  startup_id = gdk_get_startup_notification_id ();
+  if (startup_id)
+    display_wayland->startup_notification_id = g_strdup (startup_id);
 }
 
 static gboolean
index 4ac989d3de3299b7abc8f4781376ad5b9272cc45..eaa457dcd7c1efe4abd338b5c0341f17633210b6 100644 (file)
@@ -2153,19 +2153,9 @@ gdk_x11_display_make_default (GdkDisplay *display)
   g_free (display_x11->startup_notification_id);
   display_x11->startup_notification_id = NULL;
 
-  startup_id = g_getenv ("DESKTOP_STARTUP_ID");
-  if (startup_id && *startup_id != '\0')
-    {
-      if (!g_utf8_validate (startup_id, -1, NULL))
-        g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
-      else
-        gdk_x11_display_set_startup_notification_id (display, startup_id);
-
-      /* Clear the environment variable so it won't be inherited by
-       * child processes and confuse things.
-       */
-      g_unsetenv ("DESKTOP_STARTUP_ID");
-    }
+  startup_id = gdk_get_startup_notification_id ();
+  if (startup_id)
+    gdk_x11_display_set_startup_notification_id (display, startup_id);
 }
 
 static void